#include "arguments.h"
#include "../help/help.h"
#include <iostream>
#include <string>
using namespace std;

Arguments::Arguments(int argc, const char **argv) {
	for (int i = 1; i < argc; i++) {
		arguments.push_back(string((string) argv[i]));
	}
	this->argc = argc;
}

void Arguments::makepath(int opt) {
	vector<string> from;
	vector<string> to;
	int i = 1;
	int size = arguments[i].size();
	from.push_back(arguments[i]);
	WhatOption = 10;
	while (arguments[i][size - 1] == '\\' && i < argc - 1) {
		i++;
		from.push_back(arguments[i]);
		size = arguments[i].size();
		WhatOption = 10;
	}
	i++;
	if (arguments[i] == "to")
		WhatOption = opt;
	if (WhatOption == 10)
		cout << "Write 'to' after first PATH";
	if (WhatOption == opt) {
		for (int j = 0; j < (int) from.size(); j++) {
			if ((int) from.size() == 1) {
				frompath += from[j];
			} else {
				string temp;
				temp = from[j];
				temp.erase(temp.end() - 1, temp.end());
				frompath += temp;
			}
			if ((int) from.size() != 1 && j + 1 < (int) from.size())
				frompath += ' ';
		}
		i++;
		while (i < argc - 1) {
			to.push_back(arguments[i]);
			i++;
		}
		for (int j = 0; j < (int) to.size(); j++) {
			if ((int) to.size() == 1) {
				topath += to[j];
			} else {
				string temp;
				temp = from[j];
				temp.erase(temp.end() - 1, temp.end());
				topath += temp;
			}
			if ((int) to.size() != 1 && j + 1 < (int) from.size())
				topath += ' ';
		}
	}
}

void Arguments::makelist() {
	vector<string> list;
	vector<string> name;
	int i = 1;
	int size = arguments[i].size();
	list.push_back(arguments[i]);
	while (arguments[i][size - 1] == '\\' && i < argc - 1) {
		i++;
		list.push_back(arguments[i]);
		size = arguments[i].size();
	}
	for (int j = 0; j < (int) list.size(); j++) {
		if ((int) list.size() == 1) {
			listpath += list[j];
		} else {
			string temp;
			temp = list[j];
			temp.erase(temp.end() - 1, temp.end());
			listpath += temp;
		}
		if ((int) list.size() != 1 && j + 1 < (int) list.size())
			listpath += ' ';
	}
	i++;
	while (i < argc - 1) {
		name.push_back(arguments[i]);
		i++;
	}
	for (int j = 0; j < (int) name.size(); j++) {
		if ((int) name.size() == 1) {
			listname += name[j];
		} else {
			string temp;
			temp = name[j];
			temp.erase(temp.end() - 1, temp.end());
			listname += temp;
		}
		if ((int) name.size() != 1 && j + 1 < (int) name.size())
		listname += ' ';
	}
}


void Arguments::option() {
if (arguments.front() == "--help") {
	Help::getInstance().showAllHelp();
	WhatOption = 0;
} else if (arguments.front() == "-sfrom") {
	makepath(1);

} else if (arguments.front() == "-exfrom") {
	makepath(2);

} else if (arguments.front() == "-shield") {
	if (arguments.back() == "on") {
		WhatOption = 3;
	} else if (arguments.back() == "off") {
		WhatOption = 4;
	} else if (arguments.back() == "clear") {
		WhatOption = 5;
	} else if (arguments[1] == "size") {
		WhatOption = 6;
	} else if(arguments.size()==1){
		WhatOption = 12;
	} else if (arguments[1] == "extension") {
		if (arguments.size() == 4) {
			WhatOption = 7;
			if (arguments[3] == "onl" || arguments[3] == "exc") {
				arguments[2] += " " + arguments[3];
				arguments.pop_back();
			} else {
				WhatOption = 10;
				cout << "Check syntax\n";
			}
		}else cout << "Write onl or exc after extension";
	} else if (arguments[1] == "name") {
		WhatOption = 8;
		if (arguments.back() == "onl" || "exc") {
			int i = 3;
			while (i < (int) arguments.size()) {
				arguments[2] += " " + arguments[i];
				i++;
			}
		} else {
			WhatOption = 10;
			cout << "Check syntax\n";
		}
	} else if (arguments[1] == "folders") {
		WhatOption = 9;
		if (arguments.back() == "s" || "e") {
			int i = 3;
			while (i < (int) arguments.size()) {
				arguments[2] += " " + arguments[i];
				i++;
			}
	}else {
			WhatOption = 10;
			cout << "Check syntax\n";
		}
	}
} else if (arguments.front() == "-list") {
	WhatOption = 11;
	makelist();
} else {
	cout << "Use --help for more information" << endl;
	WhatOption = 10;
}
}
